﻿using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Read.API.Applications.Commands.Traceabilitys.Farmings;
using MediatR;

namespace B.S.RbacData.Read.API.Applications.CommandHandlters.Traceabilitys.Farmings
{
    /// <summary>
    /// 获取养殖企业信息命令处理
    /// </summary>

    public class GetFarmingCorporateCommandHandlter : IRequestHandler<GetFarmingCorporateCommand, ApiResult<List<FarmingCorporateModel>>>
    {
        /// <summary>
        /// 养殖信息仓储
        /// </summary>
        private readonly IBaseRepository<FarmingCorporateModel> farmingRep;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="farmingRep">养殖信息仓储</param>
        public GetFarmingCorporateCommandHandlter(IBaseRepository<FarmingCorporateModel> farmingRep)
        {
            this.farmingRep = farmingRep;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<List<FarmingCorporateModel>>> Handle(GetFarmingCorporateCommand request, CancellationToken cancellationToken)
        {

            //根据溯源编号查询养殖信息
            var farminglist = farmingRep.GetAll().Where(x => x.ProductId == request.ProductId).OrderByDescending(x => x.Creatime).ToList();

            return new ApiResult<List<FarmingCorporateModel>>
            {
                code = ResultEnumCode.Ok,
                data = farminglist,
                message = "获取养殖企业信息成功"

            };
        }



    }
}
